package com.ruoyi.bysj.service.impl;

import com.ruoyi.bysj.domain.SysEvaluation;
import com.ruoyi.bysj.domain.SysOrder;
import com.ruoyi.bysj.domain.SysStatistics;
import com.ruoyi.bysj.mapper.SysEvaluationMapper;
import com.ruoyi.bysj.mapper.SysGroupsUserMapper;
import com.ruoyi.bysj.mapper.SysOrderMapper;
import com.ruoyi.bysj.service.ISysStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysStatisticsServiceImpl implements ISysStatisticsService
{
    @Autowired
    private SysGroupsUserMapper sysGroupsUserMapper;

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysEvaluationMapper sysEvaluationMapper;

    @Override
    public SysStatistics selectSysStatisticsByUserName(String userName) {
        SysStatistics statistics = new SysStatistics();
        double averageScore = 0;
        int score = 0;
        int scoreNum =0;
        int score1 =0;
        int score2 =0;
        int score3 =0;
        int score4 =0;
        int score5 =0;
        int completeOrder =0;
        int outstandingOrder =0;
        Long groupId = sysGroupsUserMapper.selectGroupsUserByUserName(userName);
        List<Integer> orderNum = sysOrderMapper.countOrderByGroupId(groupId);
        List<Date> dates = sysOrderMapper.dateOrderByGroupId(groupId);
        statistics.setOrderNum(orderNum);
        statistics.setEndDate(dates);
        List<SysOrder> orderList = sysOrderMapper.selectSysOrderByGroupId(groupId);
        for (SysOrder sysOrder : orderList) {

            if (sysOrder.getOrderState().equals("3")||sysOrder.getOrderState().equals("4")||sysOrder.getOrderState().equals("5")||sysOrder.getOrderState().equals("6")){
                completeOrder++;
            }
            else if (sysOrder.getOrderState().equals("2")){
                outstandingOrder++;
            }
        }
        List<SysEvaluation> evaluationList = sysEvaluationMapper.selectSysEvaluationByGroupId(groupId);
        for (SysEvaluation evaluation : evaluationList) {
            score = score + Integer.valueOf (evaluation.getEvaluationScore());
            scoreNum++;
            if (evaluation.getEvaluationScore().equals("5")){
                score5++;
            }
            else if (evaluation.getEvaluationScore().equals("4")){
                score4++;
            }
            else if (evaluation.getEvaluationScore().equals("3")){
                score3++;
            }
            else if (evaluation.getEvaluationScore().equals("2")){
                score2++;
            }
            else if (evaluation.getEvaluationScore().equals("1")){
                score1++;
            }
        }
        averageScore = score/scoreNum;
        statistics.setCompleteOrder(completeOrder);
        statistics.setOutstandingOrder(outstandingOrder);
        statistics.setScore1(score1);
        statistics.setScore2(score2);
        statistics.setScore3(score3);
        statistics.setScore4(score4);
        statistics.setScore5(score5);
        statistics.setScoreNum(scoreNum);
        statistics.setAverageScore(averageScore);
        System.out.println(statistics);
        return statistics;
    }
}
